<template>
    <div class="listDiv">
        <div class="searchDiv">
            <el-select v-model="obj.req.type" placeholder="收支类型" class="search_select" clearable>
                <el-option v-for="(item,index) in globalData.financeTypeList" :key="index" :label="item.text" :value="item.value" />
            </el-select>
            <DateRange v-model:start="obj.req.starttime" v-model:end="obj.req.endtime" sph="匹配" eph="发生时间" />
            <el-input clearable placeholder="匹配明细" v-model="obj.req.key" class="search_key" />
            <span class="btn search" @click="init">搜索</span>
            <span class="btn refresh" @click="obj.req.currentpage=1;init()"></span>
            <!-- <span class="btn download" @click="export_"></span> -->
            <div class="searchRight">
                <span class="btn add" @click="add">添加财务</span>
                <span class="btn add" @click="table">生成报表</span>
            </div>
        </div>
        <div class="table_box"><div class="table_" id="tb">
            <ul class="head">
                <li class="">编号</li>
                <li class="wcen">日期</li>
                <li class="wcen">收支类型</li>
                <li class="mw300">明细</li>
                <li class="wcen">金额</li>
                <li class="mw150 wcen">操作</li>
            </ul>
            <div class="body">
                <ul v-for="x in obj.ary" :key="x">
                    <li class="">{{proxy.$utils.padding(x.finance_id,5)}}</li>
                    <li class="wcen">{{x.day}}</li>
                    <li class="wcen">
                        <div :class="[proxy.$utils.find(globalData.financeTypeList, x.type,'label')]">{{proxy.$utils.find(globalData.financeTypeList, x.type)}}</div>
                    </li>
                    <li class="mw300">
                        <el-tooltip class="box-item" effect="dark" :content="x.memo" placement="top-start" :disabled="x.memo.length<17">
                            <div class="lineOne pointer">{{x.memo}}</div>
                        </el-tooltip>
                    </li>
                    <li class="wcen">￥{{x.price}}</li>
                    <li class="mw150 wcen">
                        <el-button class="edit_btn mr10" type="success" @click="edit(x)">修改</el-button>
                        <DelConfirm class="" @yesClick="del(x)" />
                    </li>
                </ul>
            </div>
        </div>
        </div><Pager :value="obj.req" :init="init" />

        <el-dialog v-model="obj.drawer.design" :title="obj.drawer.designTitle" width="1000px" :close-on-click-modal="false" :destroy-on-close="true">
            <router-view @close="obj.drawer.design = false" @init="init" />
        </el-dialog>
        <el-dialog v-model="obj.drawer.table" :title="obj.drawer.tableTitle" width="1000px" :close-on-click-modal="false" :destroy-on-close="true">
            <Table @close="obj.drawer.table = false" />
        </el-dialog>
    </div>
</template>

<script setup>
    import { reactive, getCurrentInstance, ref, toRefs, provide, computed } from 'vue'
    import DateRange from '@/components/date_range.vue'
    import Pager from '@/components/pager.vue'
    import DelConfirm from '@/components/del_confirm.vue'
    import Table from './table.vue'
    let { proxy } = getCurrentInstance()

    let obj = reactive({
        drawer: {
            design: false,
            designTitle: "",
            table: false,
            tableTitle: "",
        },
        ary: [],
        req: {
            currentpage: 1,
            pagesize: 10,
            count: 0,
            key: "",
            type:"",
            endtime: "",
            starttime: "",
            // endtime: proxy.$utils.dateFormat(new Date(), "yyyy-MM-dd"),
            // starttime: proxy.$utils.dateAddDay(new Date(), -1, "yyyy-MM-dd"),
        }
    });
    let init = () => {
        proxy.$ajax({
            url: '/finance/list',
            data: obj.req,
            bfLoading: true
        }).then(d => {
            // console.log(d)
            if(d.success){
                obj.ary = d.msg.list
                obj.req.count = d.msg.count
            }
        }).catch(() => { })
    }

    let editId = ref(0)
    provide("id", editId); //不能加.value
    let add = () => {
        obj.drawer.design = true
        obj.drawer.designTitle = "添加财务"
        editId.value = 0;
    }
    let edit = x => {
        obj.drawer.design = true
        obj.drawer.designTitle = "修改财务"
        editId.value = x.finance_id;
    }
    let table = () => {
        obj.drawer.table = true
        obj.drawer.tableTitle = "查看报表"
    }

    let del = x => {
        proxy.$ajax({
            url: '/finance/delete',
            data: { finance_id: x.finance_id },
            bfLoading: true
        }).then(d => {
            // console.log(d)
            if(d.success){
                proxy.elMessageMixin("删除成功", "success")
                init()
            }else{
                proxy.elMessageMixin(d.err, 'error')
            }
        }).catch(() => { })
    }
</script>

<style scoped>
    ::-webkit-scrollbar { width:10px; background-color: #FFFFFF; }
    .search_select { width:100px!important; }
    .search_key { width:160px!important; }
    .btn.s { background-color: #409EFF ;}
    #tb ul li.li1 { width: 75px; }
    #tb ul li.li2 { width: 70px; }
    #tb ul li.li3 { width: 115px; }
</style>